WEBAPP['pageQuiz'] = function() {
	QUIZ.init();
};

var QUIZ = {
	totalQuizes : 2,
	curIdx : 1,
	quizType : 0,
	results : null,

	load : function() {
		$.post('/app/quiz/data/vi_kokology_source', {
			'-not-trim-space' : 1
		}, function(text) {
			var parts = text.split('=+=');
			var quizes = parts[0].split('\n|');

			$('#totalQuestions').text(QUIZ.totalQuizes = quizes.length);
			var html = [];
			var qParts = null;
			$.each(quizes, function(i, quiz) {
				html.push('<div class="quiz" id="quiz-', i, '">');

				qParts = $.trim(quiz).split('\n');
				html.push('<p class="question-index">', qParts[0], '</p>')
				html.push('<p class="question">', qParts[1], '</p>');

				for (var j = 2; j < qParts.length; j++) {
					html.push('<label><input type="radio" name="q-', i, '" value="', qParts[j].substring(0, qParts[j].indexOf('.')), '"> ', $.trim(qParts[j].substring(3)), '</label>');
				}

				html.push('</div>');
			});

			$('#quizes').html(html.join(''));
			QUIZ.ready();

			var results = parts[1].trim().split('\n||', 2);
			QUIZ.quizType = parseInt(results[0].substring(0, results[0].indexOf('\n')), 10);
			switch (QUIZ.quizType) {
			case 1:
				QUIZ.parseRs1(results[1]);
				break;
			}

		});
	},

	parseRs1 : function(results) {
		var rs = [];
		$.each(results.split('\n|'), function(i, row) {
			rs[rs.length] = row.trim().split('\n*');
		});
		QUIZ.results = rs;
	},

	init : function() {
		QUIZ.load();

		$('#helpForm').submit(function() {
			QUIZ.start();
			return false;
		});

		$('#quizes').on('click touch', '.quiz-open input', function() {
			$(this).parent().addClass('checked').parent().removeClass('quiz-open');

			if (QUIZ.curIdx < QUIZ.totalQuizes) {
				$('#quiz-' + QUIZ.curIdx).addClass('quiz-open');
				$('#currentQuestion').text(++QUIZ.curIdx);
			} else {
				QUIZ.finish();
			}
		});

		QUIZ.intro();
	},

	ready : function() {
		$('#helpForm').show();
	},
	intro : function() {
		QUIZ.showHelp();
	},

	start : function() {
		this.showPage('#page-quiz');

		$('#currentQuestion').text(QUIZ.curIdx = 1);
		$('#quizes .quiz:first').addClass('quiz-open');
	},

	finish : function() {
		var checks = $('#quizes').find('input[type="radio"]:checked');
		if (checks.length < QUIZ.totalQuizes) {
			return false;
		}

		var q = [];
		$.each(checks, function(i, item) {
			q.push(parseInt($(this).val(), 10));
		});

		this.showResult(q);

	},

	showHelp : function() {
		this.showPage('#page-help');
	},

	showResult : function(data) {
		switch (QUIZ.quizType) {
		case 1:
			QUIZ.showResult1(data);
			break;

		default:
			this.showPage('#page-result');
			break;
		}
	},
	showResult1 : function(data) {
		$.each(data, function(i, choice) {
			$('#quiz-' + i).append('<p class="result-desc">' + QUIZ.results[i][choice].substring(3).replace('\n', '<br/>') + '</p>');
		});
		$('#page-quiz').addClass('show-all');
	},

	showPage : function(page) {
		$('.app-page').hide();
		$(page).show();
	},
};